$(function () {
    var table = $("#table").dataTable({
        ajax: 'getCategoryList.html',
        columnDefs: [{
                targets: 0,
                data: function (data, type, set, meta) {
                    return data.id;
                }
            }, {
                targets: 1,
                data: function (data, type, set, meta) {
                    return data.name;
                }
            }, {
                targets: 2,
                data: function (data, type, set, meta) {
                    return data.alias;
                }
            }, {
                targets: 3,
                data: function (data, type, set, meta) {
                    return data.parentId;
                }
            }, {
                targets: 4,
                data: function (data, type, set, meta) {
                    return data.status;
                }
            }, {
                targets: 5,
                data: function (data, type, set, meta) {
                    return data.recordTime;
                }
            }, {
                targets: 6,
                data: function (data, type, set, meta) {
                    return $("#operationHTML").html().replace(/#id/g, data.id);
                }
            }],
        order: [],
        dom: 'rtip',
        drawCallback: function () {
            $(".index").off("click").on("click", function (e) {
                e.stopPropagation();
                var id = $(this).data("id");
                var type = $(this).data("type");
                if (type === "edit") {
                    location.href = "edit.html?id=" + id;
                } else {
                    $('#my-confirm').modal({
                        relatedTarget: this,
                        onConfirm: function (options) {
                            deleteItem(id);
                        },
                        // closeOnConfirm: false,
                        onCancel: function () {

                        }
                    });
                }
            });
        }
    });
    var deleteItem = function (id) {
        $.post("delete.html", {id: id}, function (e) {
            if (e) {
                table._fnAjaxUpdate();
            } else {
                alert("警告", "删除失败");
            }
        });
    };
});